package gameManager;

import static gameManager.GameOutcome.UPPER_PLAYER_WINS;

public class ExperimentsManager {
    private ExperimentsManagerGUI gui;
    
    private int upperPlayerDepth;
    private int upperPlayerHands;
    private int upperPlayerSeed;
    private int upperPlayerAlgorithm;
    
    private int downPlayerDepth;
    private int downPlayerHands;
    private int downPlayerSeed;
    private int downPlayerAlgorithm;

    private GameManager gameManager;
    
    private int upperPlayerWins;
    private int downPlayerWins;
    private int draws;
    
    public ExperimentsManager(ExperimentsManagerGUI gui){
        this.gui = gui;
        gameManager = new GameManager();
    }
    
    public void setUpperPlayerConfiguration(
            int upperPlayerDepth,
            int upperPlayerHands, 
            int upperPlayerFirstSeed,
            int upperPlayerAlgorithm){
        this.upperPlayerDepth = upperPlayerDepth;
        this.upperPlayerHands = upperPlayerHands;
        this.upperPlayerSeed = upperPlayerFirstSeed;
        this.upperPlayerAlgorithm = upperPlayerAlgorithm;
    }
    
    public void setDownPlayerConfiguration(
            int downPlayerDepth,
            int downPlayerHands, 
            int downPlayerFirstSeed,
            int downPlayerAlgorithm){
        this.downPlayerDepth = downPlayerDepth;
        this.downPlayerHands = downPlayerHands;
        this.downPlayerSeed = downPlayerFirstSeed;
        this.downPlayerAlgorithm = downPlayerAlgorithm;
    }
    
    public void run(int numGames, int dealerSeed){
        
        upperPlayerWins = downPlayerWins = draws = 0;
        for (int i = 0; i < numGames; i++) {
            System.gc();
            System.out.print("jogo "+(i+1)+"....");
            gameManager.createUpperPlayer(upperPlayerDepth, upperPlayerHands, upperPlayerSeed++, upperPlayerAlgorithm);

            gameManager.createDownPlayer(downPlayerDepth, downPlayerHands, downPlayerSeed++, downPlayerAlgorithm);

            GameOutcome outcome = gameManager.playGame(dealerSeed++);
            
            switch(outcome){
                case UPPER_PLAYER_WINS: upperPlayerWins++; break;
                case DOWN_PLAYER_WINS:  downPlayerWins++;  break;
                default: draws++;
            }
            System.out.println(" Acabou!");
        }
        
        gui.showResults(
                "Upper player wins: " + upperPlayerWins + 
                "\nDown player wins: " + downPlayerWins + 
                "\ndraws: " + draws);
    }
}
